package app.imttf.internal.backing.login;

import app.imttf.internal.adfextensions.ADFUtils;
import app.imttf.internal.adfextensions.JSFUtils;

import app.imttf.internal.backing.admin.user.UserData;

import java.util.HashMap;
import java.util.Map;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.binding.OperationBinding;

public class LoginBean {

    private RichInputText inputLogin, inputPassword;
    private String _action;

    public String loginAction() {

        _action = null;

        OperationBinding login = ADFUtils.findOperation("authenticateUser");
        Map m = (Map)login.execute();

        try {
            if (m != null && !m.isEmpty()) {

                HashMap userAccess = (HashMap)m.get("UserAccess");
                String fullName = (String)m.get("FullName");
                String userNameLogin = (String)m.get("UserName");
                String password = (String)m.get("Password");
                String companyName = (String)m.get("CompanyName");
                String kodeDc = (String)m.get("KodeDc");
                String userType = (String)m.get("userType");

                UserData userData =
                    (UserData)JSFUtils.resolveExpression("#{UserData}");
                userData.setLoggedIn(Boolean.TRUE);
                userData.setUserAccess(userAccess);
                userData.setFullName(fullName);
                userData.setUserNameLogin(userNameLogin);
                userData.setPassword(password);
                userData.setCompanyName(companyName);
                userData.setKodeDc(kodeDc);
                userData.setUserType(userType);

                OperationBinding _menuAMSession = ADFUtils.findOperation("setLoginToSession_MenuAM");
                _menuAMSession.execute();

                OperationBinding _administratorAMSession = ADFUtils.findOperation("setLoginToSession_AdministratorAM");
                _administratorAMSession.execute();

                _action = "success";
            } else {
                _action = "errlogin";
            }
        } catch (Exception e) {
            _action = "errglobal";
        }

        return _action;
    }

    public void setInputLogin(RichInputText inputLogin) {
        this.inputLogin = inputLogin;
    }

    public RichInputText getInputLogin() {
        return inputLogin;
    }

    public void setInputPassword(RichInputText inputPassword) {
        this.inputPassword = inputPassword;
    }

    public RichInputText getInputPassword() {
        return inputPassword;
    }

}
